﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using FaWorksSolution.MvcApplication.Models.Assets;

namespace FaWorksSolution.MvcApplication.API.Controllers
{
    public class CdnController : ApiControllerBase
    {
        // GET api/Cdn
        public IEnumerable<Cdn> GetCdns(Guid fileId)
        {
            var file = db.AssetsFiles.Include("Cdns").FirstOrDefault(m => m.FileId == fileId);
            if (file == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return file.Cdns.AsEnumerable();
        }

        // GET api/Cdn/5
        public Cdn GetCdn(Guid id)
        {
            Cdn cdn = db.AssetsCdns.Find(id);
            if (cdn == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return cdn;
        }

        // PUT api/Cdn/5
        public HttpResponseMessage PutCdn(Guid id, Cdn cdn)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != cdn.CdnId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(cdn).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Cdn
        public HttpResponseMessage PostCdn(Cdn cdn)
        {
            var fileId = HttpContext.Current.Request.Params["fileId"];
            if (string.IsNullOrEmpty(fileId))
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "缺少参数fileId");
            }
            var file = db.AssetsFiles.Find(new Guid(fileId));
            if (file == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            if (ModelState.IsValid)
            {
                cdn.CdnId = Guid.NewGuid();
                file.Cdns = new List<Cdn>();
                file.Cdns.Add(cdn);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, cdn);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = cdn.CdnId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/Cdn/5
        public HttpResponseMessage DeleteCdn(Guid id)
        {
            Cdn cdn = db.AssetsCdns.Find(id);
            if (cdn == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.AssetsCdns.Remove(cdn);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, cdn);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        private DefaultContext db = new DefaultContext();
    }
}